using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;

using NereusGame.Entities.Weapons;
#endif

namespace NereusGame.Entities.Ships.Enemies
{
    public partial class EnemyDefender
    {

        private float resetDistance;
        private Vector3 directionToSecondaryTarget;
        private Vector3 directionToPtimaryTarget;
        private Vector3 directionToTertiaryTarget;
        private const int MAX_WEAPONS = 1;
        public const float MAX_HEALTH = 200f;      // Initial Health of the player.
        private const float BODY_RADIUS = 2f;
        private void CustomInitialize()
        {
            InitializeMiner();
            InitializeWeApons();

        }

        private void CustomActivity()
        {

        }

        private void CustomDestroy()
        {
            base.Destroy();

        }

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }


        private void InitializeMiner()
        {

            Tex.Texture = defenderTexture;
            Tex.ScaleX = Tex.ScaleY = Body.Radius = BODY_RADIUS;
            CurrentHealth = MaxHealth = MAX_HEALTH;
            HealthBar.RelativePosition = new Vector3(0, Body.Radius + HEALTHBAR_OFFSET, 0);
            directionToTertiaryTarget = directionToPtimaryTarget = directionToSecondaryTarget = Vector3.Zero;
            PrimaryTargetRange = 50f;
            SecondaryTargetRange = TertiaryTargetRange = 60f;
            resetDistance = SecondaryTargetRange * 1.8f;
            points = 500;

        }

        private void InitializeWeApons()
        {
            weaponInventory = new Weapon[MAX_WEAPONS];
            weaponInventory[0] = new Gun(CONTEN_MANAGER);
            weaponInventory[0].UpgradeWeapon();    // set gun's level to 1
            weaponInventory[0].UpgradeWeapon();    // set gun's level to 2
            weaponInventory[0].UpgradeWeapon();
            weaponInventory[weaponSelector].SetFireRate(34);
            weaponSelector = 0;
        }


        protected override void NormalState()
        {
            if (PrimaryTarget == null || !PrimaryTarget.IsAlive())
            {
                state = EnemyState.Angry;
                return;
            }
            else
            {
                if ((Game1.mGameMode == Game1.GameMode.Arena))
                {

                    if (((SecondaryTarget.Position - PrimaryTarget.Position).Length() <= PrimaryTargetRange) || ((TertiaryTarget.Position - PrimaryTarget.Position).Length() <= PrimaryTargetRange))
                    {
                        state = EnemyState.Angry;
                        return;
                    }
                }
                else
                {
                    if ((SecondaryTarget.Position - PrimaryTarget.Position).Length() <= PrimaryTargetRange)
                    {
                        state = EnemyState.Angry;
                        return;
                    }
                }
            }



            if ((Position - PrimaryTarget.Position).Length() <= PrimaryTargetRange)
            {
                Drag = FRICTION * 4.5f;
                Acceleration = Vector3.Zero;
            }
            else
            {
                Drag = FRICTION;
                MoveTowardsAsteroid();

            }
        }

        protected override void AngryState()
        {
            Drag = FRICTION;
            // if the player is too far then go back to asteroid
            if (PrimaryTarget != null && PrimaryTarget.IsAlive())
            {
                // check for arena mode
                if (Game1.mGameMode == Game1.GameMode.Arena)
                {
                    // comapre both player's distance
                    if (((Position - SecondaryTarget.Position).Length() > resetDistance) || (Position - TertiaryTarget.Position).Length() > resetDistance)
                    {
                        state = EnemyState.Normal;
                        return;
                    }
                }
                // single player mode
                else
                {
                    if (((Position - SecondaryTarget.Position).Length() > resetDistance))
                    {
                        state = EnemyState.Normal;
                        return;
                    }
                }

            }

            float secondaryTargetDist = (Position - SecondaryTarget.Position).Length();

            // chase the palyer 
            if (Game1.mGameMode == Game1.GameMode.Arena)
            {
                float tertiaryTargetDist = (Position - TertiaryTarget.Position).Length();

                if (secondaryTargetDist > tertiaryTargetDist)
                {

                    if (secondaryTargetDist <= SecondaryTargetRange * 0.5f)
                    {
                        Acceleration = Vector3.Zero;
                        ShootPlayer1();
                    }
                    else
                    {
                        if (secondaryTargetDist < (resetDistance) * 0.85f)
                            ShootPlayer1();
                        MoveTowardsPlayer1();
                    }
                }
                else
                {
                    if (tertiaryTargetDist <= TertiaryTargetRange * 0.5f)
                    {
                        Acceleration = Vector3.Zero;
                        ShootPlayer2();
                    }
                    else
                    {
                        if (tertiaryTargetDist < (resetDistance) * 0.85f)
                            ShootPlayer2();
                        MoveTowardsPlayer2();
                    }
                }
            }
            else
            {
                if (secondaryTargetDist <= SecondaryTargetRange * 0.5f)
                {
                    Acceleration = Vector3.Zero;
                    ShootPlayer1();
                }
                else
                {
                    if (secondaryTargetDist < (resetDistance) * 0.85f)
                        ShootPlayer1();
                    MoveTowardsPlayer1();
                }
            }

        }



        private void ShootPlayer1()
        {
            weaponInventory[weaponSelector].SetFireRate(40);

            directionToSecondaryTarget = SecondaryTarget.Position - Position;
            directionToSecondaryTarget.Normalize();

            if (directionToSecondaryTarget.X >= 0)
                RotationZ = -(float)Math.Acos(directionToSecondaryTarget.Y);
            else
                RotationZ = (float)Math.Acos(directionToSecondaryTarget.Y);



            weaponInventory[weaponSelector].Shoot(this, Position, (BODY_RADIUS + 1f), RotationZ, Velocity);

        }


        private void MoveTowardsPlayer1()
        {
            directionToSecondaryTarget = SecondaryTarget.Position - Position;
            directionToSecondaryTarget.Normalize();
            Acceleration = directionToSecondaryTarget * 24f;
        }


        private void ShootPlayer2()
        {
            weaponInventory[weaponSelector].SetFireRate(40);

            directionToTertiaryTarget = TertiaryTarget.Position - Position;
            directionToTertiaryTarget.Normalize();

            if (directionToTertiaryTarget.X >= 0)
                RotationZ = -(float)Math.Acos(directionToTertiaryTarget.Y);
            else
                RotationZ = (float)Math.Acos(directionToTertiaryTarget.Y);



            weaponInventory[weaponSelector].Shoot(this, Position, (BODY_RADIUS + 1f), RotationZ, Velocity);

        }


        private void MoveTowardsPlayer2()
        {
            directionToTertiaryTarget = TertiaryTarget.Position - Position;
            directionToTertiaryTarget.Normalize();
            Acceleration = directionToTertiaryTarget * 24f;
        }

        private void MoveTowardsAsteroid()
        {
            directionToPtimaryTarget = PrimaryTarget.Position - Position;
            directionToPtimaryTarget.Normalize();
            Acceleration = directionToPtimaryTarget * 17f;
        }
    }
}
